package client;
/**
 * @mainpage User's Manual
 * 
 * 			Client: for using client, start the application from the console. Input the adress and the port of server<br>
 * 			in your LAN. When connected, input the number you wish to factor, check if is prime or find the smallest<br>
 * 			divider and apply with a certein button. To exit, use exit button.<br>
 * 
 * 			Server: run with arguments [port] [largest number]. Will be able to answer requests about numbers equal to or <br>
 * 			lower than [largest number].
 */
import java.awt.Color;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
/**
 * Public class. Includes GUI and connection establishing
 * @author agurgul
 * @version 1.0.0
 */
public class Client {
	
	public static ClientThread clientThread;
	
	public static JFrame startFrame;
	public static JLabel portLabel;
	public static JLabel adressLabel;
	public static JButton connectButton;
	public static JButton exitButton;
	public static JTextField portIn;
	public static JTextField adressIn;
	public static JPanel mainPanel;
	
	public static JFrame businessFrame;
	public static JPanel buttonsPanel;
	public static JPanel screenPanel;
	public static JLabel screen;
	public static JTextField inputField;
	public static JButton factorisation;
	public static JButton isPrime;
	public static JButton lowestDiv;
	public static JButton exitButton2;
	public static TableGUI tableGUI;
	
	public static void main(String[] args) {
		GUIThread guiThread = new GUIThread();
		SwingUtilities.invokeLater(guiThread);

	}
	/**
	 * Connect method.
	 */
	public static void connect(){
		try{
			String port = portIn.getText();
			String adress = adressIn.getText();
			clientThread = new ClientThread(adress, port);
			Thread clientTask = new Thread(clientThread);
			int slot = Integer.parseInt(clientThread.getInput().readLine());
			int sb = Integer.parseInt(clientThread.getInput().readLine());
			int bb = Integer.parseInt(clientThread.getInput().readLine());
			int balance = Integer.parseInt(clientThread.getInput().readLine());
			startFrame.setVisible(false);
			tableGUI = new TableGUI(20, 20, 100, 1);
			tableGUI.newgame(sb, bb, balance);
			tableGUI.sitDown(slot);
			tableGUI.setVisible(true);

			//TODO trzeba dodac tutaj uruchomienie jakiegos watku, ktory bedzie czytac w sposob ciagly linijki od serwera i wykonywac metody.
		}catch(Exception ex){
			JOptionPane.showMessageDialog(startFrame, "Nieprawidlowy adres lub port. Sprobuj ponownie.", "Blad!", JOptionPane.WARNING_MESSAGE);
			portIn.setText("");
			adressIn.setText("");
		}
	}

	public static void disconnect() {
		clientThread.getOutput().println( "Q" );
	}

	public static void play(){
		Thread writer = new Thread(new Runnable() {
			public void game(){
				String[] news = new String[6];
				try{
					for (int i = 0; i < 6; i++){
						news[i] = Client.clientThread.getInput().readLine();
					}
				}catch(IOException e){
					System.out.println("IOE game");
				};
				try{
					int id = Integer.parseInt(news[0]);
					boolean changingCards = Boolean.parseBoolean(news[1]);
					int cards = Integer.parseInt(news[2]);
					char action = news[3].charAt(0);
					int bid = Integer.parseInt(news[4]);
					int balance = Integer.parseInt(news[5]);
					for( String s : news )
						System.out.println( s );
					Client.tableGUI.someonesMove(id, changingCards, cards, action, bid, balance);
				}catch(NumberFormatException e){
					System.out.println("NumberFormatException game");
				}
				
			}
			public void winners(){
				String[] news = new String[3];
				try{
					for (int i = 0; i < 3; i++){
						news[i] = Client.clientThread.getInput().readLine();
					}
				}catch(IOException e){
					System.out.println("IOE winners");
				};
				try{
					int id = Integer.parseInt(news[0]);
					int win = Integer.parseInt(news[1]);
					int balance = Integer.parseInt(news[2]);
					Client.tableGUI.winners(id, win, balance);
				}catch(NumberFormatException e){
					System.out.println("NFE winners");
				}
			}
			public void cards(){
				String[] news = new String [9];
				try{
					for (int i = 0; i < 9; i++){
						news[i] = Client.clientThread.getInput().readLine();
					}
					System.out.println( news[ 0 ] );
				}catch(IOException e){
					System.out.println("IOE cards");
				}
				try{
					int id = Integer.parseInt(news[0]);
					String rank1 = news[1];
					String suit1 = news[2];
					String rank2 = news[3];
					String suit2 = news[4];
					String rank3 = news[5];
					String suit3 = news[6];
					String rank4 = news[7];
					String suit4 = news[8];
					for( String s : news ) {
						System.out.println( s );
					}
					Client.tableGUI.cards(id, rank1, suit1, rank2, suit2, rank3, suit3, rank4, suit4);
				}catch(NumberFormatException e){
					System.out.println("NFE cards");
				}
			}
			public void setUp(){
				String[] news = new String[4];
				try{
					for (int i = 0; i < 4; i++){
						news[i] = Client.clientThread.getInput().readLine();
					}
				}catch(IOException e){
					System.out.println("IOE setup");
				};
				try{
					int id = Integer.parseInt(news[0]);
					String status= news[1];
					int bid = Integer.parseInt(news[2]);
					int balance = Integer.parseInt(news[3]);
					Client.tableGUI.setup(id, status, bid, balance);
				}catch(NumberFormatException e){
					System.out.println("NFE setup");
				}
			}
			public void yourTurn(){
				String news;
				
				try{
					news = Client.clientThread.getInput().readLine();
					if ( news.equals("You bid.") ){
						String news2 = Client.clientThread.getInput().readLine();
						Client.tableGUI.bidTime(news2);
					}else{
						Client.tableGUI.changeCardsTime();
					}
				}catch(IOException e){
					System.out.println("IOE yt");
				};
				
			}
			@Override
			public void run() {
				while (true){
					try{
						String command = Client.clientThread.getInput().readLine();
						System.out.println( command );
						switch (command) {
							case "Winners":
								winners();
								break;
							case "Game":
								game();
								break;
							case "SetUp":
								setUp();
								break;
							case "Cards":
								cards();
								break;
							case "YourTurn":
								yourTurn();
							default:
								break;
						}
					}
					catch(IOException e){
						System.out.println( "I/O error." );
					}
					catch( NullPointerException e ) {
						System.out.println( "NullPointerException." );
						return;
					}
				}
			}
		});
		writer.start();
	}

}
